<template>
  <div class="login-container">
    <div class="logo">
      <img src="../../assets/logo.png" alt="">
    </div>
    <div class="login_box">
      <div class="login_title">
        <p>欢迎登录</p>
        <p><span>OpenGADL</span> 管理平台</p>
      </div>
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
        <el-form-item prop="loginAccount">
          <el-input
            ref="loginAccount"
            v-model="loginForm.loginAccount"
            placeholder="用户名"
            name="loginAccount"
            type="text"
            auto-complete="on"
          >
          <template slot="prepend"><img src="../../assets/user.png" class="icon1" alt=""></template></el-input>
        </el-form-item>
        <el-form-item prop="loginPassWord">
          <el-input
            :key="passwordType"
            ref="loginPassWord"
            v-model="loginForm.loginPassWord"
            :type="passwordType"
            placeholder="密码"
            name="loginPassWord"
            tabindex="2"
            auto-complete="on"
            @keyup.enter.native="handleLogin"
          >
          <template slot="prepend"><img src="../../assets/password.png" class="icon2" alt=""></template></el-input>
        </el-form-item>
  
        <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">登录</el-button>
  
      </el-form>
    </div>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { login, logout, getInfo, getNewToken } from '@/api/user'
export default {
  name: 'Login',
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码长度不正确'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        loginAccount: '',
        loginPassWord: ''
      },
      loginRules: {
        loginAccount: [{ required: true, trigger: 'blur',message:'请输入用户名' }],
        loginPassWord: [{ required: true, trigger: 'blur', message:'请输入密码' }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          setToken('szerp-loginToken', '123')
          // this.$router.push({ path: '/' })
          console.log(123)
          this.$store.dispatch('user/login', this.loginForm).then(() => {
            this.$router.push({ path: '/' })
            // location.reload(true)
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */


</style>

<style lang="scss" scoped>

::v-deep .el-button--primary{
  background-color: rgba(30, 144, 255, 1);
  border-color: rgba(30, 144, 255, 1);
}

.login-container {
  display: flex;
  min-height: 100%;
  width: 100%;
  overflow: hidden;
  background-image: url('../../assets/后台登录-bg.png');

  .logo{
    flex: 1;
    padding-left: 43px;
    padding-top: 29px;
    img{
      width: 170px;
      height: 70px;
    }
  }

  .login_box{
    padding-top: 319px;
    width: 700px;
    background-color: #fff;
    min-height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    .login_title{
      margin-bottom: 30px;
      p{
        text-align: center;
        font-size: 32px;
        font-weight: 700;
        height: 64px;
        line-height: 64px;
        span{
          color: rgba(30, 144, 255, 1);
        }
      }
    }
  }
  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 16px;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .icon1{
    width: 17px;
  }

  .icon2{
    width: 16px;
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    cursor: pointer;
    user-select: none;
  }
}
</style>
